//
//  ResetPasswordViewController.m
//  Daybook
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "Common.h"
#import <BmobSDK/Bmob.h>

@interface ResetPasswordViewController (){
    UITextField *userPhoneNumberTextField;
    UITextField *captchaTextField;
    UITextField *passwordTextField;
    UITextField *verityPasswordTextField;
    UIButton *getCaptchaButton;
    NSTimer *countDownTimer;
    unsigned secondsCountDown;
}
@property UITextField *userPhoneNumberTextField;
@property UITextField *captchaTextField;
@property UITextField *passwordTextField;
@property UITextField *verityPasswordTextField;
@property UIButton *getCaptchaButton;
@property NSTimer *countDownTimer;
@property unsigned secondsCountDown;
@end

@implementation ResetPasswordViewController
@synthesize userPhoneNumberTextField;
@synthesize captchaTextField;
@synthesize passwordTextField;
@synthesize verityPasswordTextField;
@synthesize getCaptchaButton;
@synthesize countDownTimer;
@synthesize secondsCountDown;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [self createView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)createView {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 34, 25, 25);
    [self.view addSubview:backButton];
    backButton.backgroundColor = [UIColor clearColor];
    
    [backButton setImage:[UIImage imageNamed:@"backArrow"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //    UIButton *registerButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    //    registerButton.frame = CGRectMake(MAIN_WIDTH - 50, 34, 40, 25);
    //    registerButton.backgroundColor = [UIColor whiteColor];
    //    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    //    [registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    //    [registerButton addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:registerButton];
    
    
    self.userPhoneNumberTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, MAIN_WIDTH - 20, 40)];
    self.userPhoneNumberTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.userPhoneNumberTextField];
    
    self.userPhoneNumberTextField.placeholder = @"手机号";
    
    
    self.getCaptchaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.getCaptchaButton.frame = CGRectMake(MAIN_WIDTH - 80, 105, 60, 30);
    self.getCaptchaButton.backgroundColor = [UIColor lightGrayColor];
    [self.getCaptchaButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.getCaptchaButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.getCaptchaButton.titleLabel.font = [UIFont systemFontOfSize:11];
    
    self.getCaptchaButton.layer.cornerRadius = 5;
    self.getCaptchaButton.layer.masksToBounds = YES;
    [self.getCaptchaButton addTarget:self action:@selector(getCaptch:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.getCaptchaButton];
    
    
    
    
    self.captchaTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 140, MAIN_WIDTH - 20, 40)];
    self.captchaTextField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.captchaTextField];
    self.captchaTextField.placeholder = @"请输入验证码";
    
    
    
    
    
    self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 180, MAIN_WIDTH - 20, 40)];
    self.passwordTextField.backgroundColor = [UIColor whiteColor];
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextField];
    
    
    
    
    
    UIView *firstLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, MAIN_WIDTH, 1)];
    firstLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:firstLineView];
    
    UIView *secLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 140, MAIN_WIDTH, 1)];
    secLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:secLineView];
    
    
    UIView *thirdLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 180, MAIN_WIDTH, 1)];
    thirdLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:thirdLineView];
    
    UIView *fourLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 220, MAIN_WIDTH, 1)];
    fourLineView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:fourLineView];
    
    
    UIButton *finishedRegisterButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    finishedRegisterButton.frame = CGRectMake(10, 240, MAIN_WIDTH - 20, 40);
    finishedRegisterButton.backgroundColor  = [UIColor orangeColor];
    [self.view addSubview:finishedRegisterButton];
    
    [finishedRegisterButton setTitle:@"重置密码" forState:UIControlStateNormal];
    [finishedRegisterButton addTarget:self action:@selector(setResetPasswordBtnLogic:) forControlEvents:UIControlEventTouchUpInside];
    finishedRegisterButton.layer.cornerRadius = 5;
    finishedRegisterButton.layer.masksToBounds = YES;
    
    
    
    
    
}



- (void)getCaptch:(UIButton *)button {
    
    //获取手机号
    BmobUser *user = [BmobUser getCurrentUser];
    NSString *mobilePhoneNumber = user.mobilePhoneNumber;
    
    //请求验证码
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:mobilePhoneNumber andTemplate:@"test" resultBlock:^(int number, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            //获得smsID
            NSLog(@"sms ID：%d",number);
            //设置不可点击
            [self setRequestSmsCodeBtnCountDown];
        }
    }];

    
}



-(void)setRequestSmsCodeBtnCountDown{
    [self.self.getCaptchaButton setEnabled:NO];
    self.self.getCaptchaButton.backgroundColor = [UIColor grayColor];
    self.secondsCountDown = 60;
    
    countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTimeWithSeconds:) userInfo:nil repeats:YES];
    [countDownTimer fire];
}


-(void)countDownTimeWithSeconds:(NSTimer*)timerInfo{
    if (secondsCountDown == 0) {
        [self.getCaptchaButton setEnabled:YES];
        self.getCaptchaButton.backgroundColor = [UIColor redColor];
        [self.getCaptchaButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [countDownTimer invalidate];
    } else {
        [self.getCaptchaButton setTitle:[[NSNumber numberWithInt:secondsCountDown] description] forState:UIControlStateNormal];
        self.secondsCountDown--;
    }
}



# pragma mark - 逻辑



-(void)setResetPasswordBtnLogic{
    NSString *smsCode = self.captchaTextField.text;
    NSString *password = self.passwordTextField.text;
    NSString *verifyPassword = self.verityPasswordTextField.text;
    if (!smsCode || [smsCode isEqualToString:@""] || !password || [password isEqualToString:@""] || !verifyPassword || [verifyPassword isEqualToString:@""] || ![password isEqualToString:verifyPassword]) {
        UIAlertView *tip = [[UIAlertView alloc] initWithTitle:nil message:@"验证码不能为空，密码及确认密码必须相等且不为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [tip show];
    } else {
        
        [BmobUser resetPasswordInbackgroundWithSMSCode:smsCode andNewPassword:password block:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                UIAlertView *tip = [[UIAlertView alloc] initWithTitle:nil message:@"重置密码成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [tip show];
                //跳转
                self.presentingViewController.view.alpha = 0;
                self.presentingViewController.presentingViewController.view.alpha = 0;
                [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
            } else {
                UIAlertView *tip = [[UIAlertView alloc] initWithTitle:nil message:@"验证码有误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [tip show];
            }
        }];
    }
}


- (void)clickBack:(UIButton *)button {
    
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


@end
